package cn.edu.njtc.controller;

import cn.edu.njtc.common.R;
import cn.edu.njtc.entity.dto.UserAddDto;
import cn.edu.njtc.entity.dto.UserDto;
import cn.edu.njtc.entity.dto.UserModifyDto;
import cn.edu.njtc.entity.dto.UserTokenDto;
import cn.edu.njtc.entity.pojo.User;
import cn.edu.njtc.entity.vo.UserVo;
import cn.edu.njtc.exception.BizException;
import cn.edu.njtc.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static cn.edu.njtc.common.R.ok;

@RestController
@RequestMapping("/user")
@Tag(name = "用户管理",description = "用户管理接口")
public class UserController {

    private final UserService userService;
    private final User user;

    @Autowired
    public UserController(UserService userService, User user) {
        this.userService = userService;
        this.user = user;
    }


    /**
     * @RequestMapping:请求的映射，声明方法对应的资源名称，支持多种请求方式（post/get/delete/put）
     * @GetMapping: 以GET方式响应请求
     * @PostMapping: 以Post方式响应请求
     * @RequestParam:接收前端发送请求的时的参数名，如果和方法的参数名一致，可省略
     */
    @Operation(summary = "获取用户详情", description = "根据ID查询用户")
    @Parameters(
            {
                    @Parameter(name="id",example = "1")
            }
    )
    @GetMapping("/{id}")
    public R<UserVo> selectById(@PathVariable Integer id) throws BizException {
            User user = userService.selectById(id);
            UserVo userVo = new UserVo();
            BeanUtils.copyProperties(user,userVo);
            return  R.ok(userVo);
    }


    @Operation(summary = "用户注册", description = "手机号注册")
    @PostMapping("/register")
    public R<Integer> register(@RequestBody UserDto userDto) throws BizException {
        int register = userService.register(userDto);
        return R.ok(register);
    }


    // 如果用浏览器(get)访问post请求，就会提示http状态码：405（请求方式不正确）
    // There was an unexpected error (type=Method Not Allowed, status=405)
    // 可以用接口的测试工具 postman/apifox/swagger

    @Operation(summary = "用户登录", description = "用户名密码登录")
    @PostMapping("/login")
    public R<UserTokenDto> login(@RequestBody UserDto userDto) throws BizException {
        UserTokenDto userTokenDto = userService.login(userDto);
        return R.ok(userTokenDto);
    }

    @Operation(summary = "添加用户", description = "添加用户")
    @PostMapping("/")
    public R<Integer> add(@RequestBody UserAddDto userAddDto){
        User user = new User();
        BeanUtils.copyProperties(userAddDto,user);
        int add = userService.add(user);
        return R.ok(add);
    }

    @Operation(summary = "查询用户", description = "姓名模糊查询")
    @GetMapping("/")
    public R<List<User>> selectLikeName(String name){
        List<User> users = userService.selectLikeName(name);
        return R.ok(users);
    }


    @Operation(summary = "删除用户", description = "根据id删除用户")
    @PostMapping("/{id}")
    public R<Integer> deleteById(@PathVariable Integer id){
        int i = userService.deleteById(id);
        return R.ok(i);
    }


    @Operation(summary = "修改信息", description = "修改用户信息")
    @PostMapping("/modify")
    public R<Integer> modify(@RequestBody UserModifyDto userModifyDto){
        User user = new User();
        BeanUtils.copyProperties(userModifyDto,user);
        int modify = userService.modify(user);
        return R.ok(modify);
    }


}
